package com.guoheng.safe.sys.service.impl;

import com.guoheng.safe.common.base.Result;
import com.guoheng.safe.common.constant.CommonConstants;
import com.guoheng.safe.common.enums.ErrorCodeEnum;
import com.guoheng.safe.sys.dto.basicinfo.BasicInfoDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.guoheng.safe.sys.model.basicinfo.BasicInfo;
import com.guoheng.safe.sys.mapper.basicinfo.BasicInfoMapper;
import com.guoheng.safe.sys.service.BasicInfoService;
/**
 * 类功能描述:　BasicInfoServiceImpl
 *
 * @author Eternal
 * @date 2019-00-15 15:00
 */
@Service
public class BasicInfoServiceImpl implements BasicInfoService{

    @Resource
    private BasicInfoMapper basicInfoMapper;

    @Override
    public Result getBasicInfo() {
        BasicInfo basicInfo = basicInfoMapper.getBasicInfo();
        return Result.success(basicInfo);
    }

    @Override
    public Result addBasicInfo(BasicInfoDTO basicInfoDTO) {
        if(null != basicInfoMapper.getBasicInfo()){
            return Result.failure(ErrorCodeEnum.SYS_ERR_BASIC_INFO_EXISTENT);
        }
        BasicInfo basicInfo = new BasicInfo();
        BeanUtils.copyProperties(basicInfoDTO, basicInfo);
        int result = basicInfoMapper.insertBasicInfo(basicInfo);
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_CREATE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result updateBasicInfo(BasicInfoDTO basicInfoDTO) {
        BasicInfo basicInfo = new BasicInfo();
        BeanUtils.copyProperties(basicInfoDTO, basicInfo);
        int result = basicInfoMapper.updateBasicInfo(basicInfo);
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_UPDATE_FAILED);
        }
        return Result.success();
    }
}
